import { ActionContext } from "vuex";
import {
  PlayerInfo,
  PlayerStatusOption,
  PlayerStatusOptionType
} from "./types";
import { RootState } from "../types";
import { PlayerActionTypes } from "./action-types";
import { MutationTypes } from "./mutation-types";
import { Equipment } from "@/const/equips/equipmentType";
import { equiptoPackItem } from "@/const/equips/equipmentType";
import { SysActionTypes } from "../sys/action-types";
import { SysInfo } from "../sys/types";

export type Actions<S = RootState, R = RootState> = {
  [PlayerActionTypes.PLAYER_LOGIN_USER](
    { commit }: ActionContext<S, R>,
    data: PlayerInfo
  ): Promise<void>;
  [PlayerActionTypes.PLAYER_WEAR_EQUIP](
    { state, commit }: ActionContext<S, R>,
    data: Equipment
  ): Promise<void>;
  [PlayerActionTypes.PLAYER_CAST_EQUIP](
    { commit }: ActionContext<S, R>,
    data: Equipment
  ): Promise<void>;
  [PlayerActionTypes.PLAYER_SET_STATUS](
    { commit, dispatch, getters }: ActionContext<S, R>,
    data: PlayerStatusOption
  ): Promise<void>;
  [PlayerActionTypes.PLAYER_REVIVE]({
    commit,
    dispatch
  }: ActionContext<S, R>): Promise<void>;
};

export const playerActions: Actions = {
  async [PlayerActionTypes.PLAYER_LOGIN_USER]({ commit }, data) {
    commit(MutationTypes.SET_USER_INFO, data);
  },
  async [PlayerActionTypes.PLAYER_WEAR_EQUIP]({ state, commit }, data) {
    const { userWearableEquipments } = state;
    const { type } = data;
    const wearItem = userWearableEquipments[type];
    if (wearItem) {
      commit(MutationTypes.CAST_EQUIP, wearItem);
      const packItem = equiptoPackItem(wearItem);
      commit(MutationTypes.PUT_ITEM_TO_PACKAGE, packItem);
    }
    commit(MutationTypes.WEAR_EQUIP, data);
    commit(MutationTypes.REMOVE_ITEM_TO_PACKAGE, data.id);
  },
  async [PlayerActionTypes.PLAYER_CAST_EQUIP]({ commit }, data) {
    commit(MutationTypes.CAST_EQUIP, data);
    const packItem = equiptoPackItem(data);
    commit(MutationTypes.PUT_ITEM_TO_PACKAGE, packItem);
  },
  async [PlayerActionTypes.PLAYER_SET_STATUS](
    { commit, dispatch, getters },
    data
  ) {
    switch (data.type) {
      case PlayerStatusOptionType.HP: {
        const CHP = getters.userPropertyCHP;
        const HP = getters.playerBattleProperty.HP;
        const computedValue = Math.min(Math.max(CHP + data.value, 0), HP);
        commit(MutationTypes.SET_CURRENT_HP, computedValue);
        if (!computedValue) {
          dispatch(SysActionTypes.SYS_PUSH_INFO, new SysInfo(`你狗带了`, 1));
          dispatch(PlayerActionTypes.PLAYER_REVIVE);
        }
        break;
      }
      default:
        break;
    }
  },
  async [PlayerActionTypes.PLAYER_REVIVE]({ commit, dispatch }) {
    const revivalTime = 10;
    dispatch(SysActionTypes.SYS_PUSH_INFO, new SysInfo(`10秒后复活`, 1));
    let time = 0;
    const timer = setInterval(() => {
      if (++time === 10) {
        commit(MutationTypes.SET_USER_STATE, 0);
        dispatch(PlayerActionTypes.PLAYER_SET_STATUS, {
          type: PlayerStatusOptionType.HP,
          value: 100
        });
        dispatch(
          SysActionTypes.SYS_PUSH_INFO,
          new SysInfo(`不死的小强又站起来啦！`, 1)
        );
        clearInterval(timer);
      } else {
        dispatch(
          SysActionTypes.SYS_PUSH_INFO,
          new SysInfo(`${revivalTime - time}秒后复活`, 1)
        );
      }
    }, 1000);
  }
};
